<template>
	<view>
		<uv-sticky bgColor="#fff">
			<uv-tabs v-if="theme=='fs'" :scrollable="false" :current="currentType" :list="list"
				@change="onChange"></uv-tabs>
			<uv-line></uv-line>
			<uv-tabs :lineColor="theme=='fs'?'#006BFE':'#0BCA89'" :scrollable="false" :list="subList"
				:current="subCurrent" @change="onSubChange"></uv-tabs>
		</uv-sticky>

		<scroll-view class="list-box" :scroll-y="true" :enable-back-to-top="true" :refresher-enabled="true"
			:refresher-triggered="triggered" refresher-background="#f1f1f1" :lower-threshold="40"
			@scrolltolower="onScrolltolower" @refresherrefresh="onRefresh" @refresherabort="onAbort">
			<view class="trim-box"></view>
			<ItemCard v-for="(item, index) in dataList" :dataItem="item" :key="index" :inOrder="true"
				:orderStatus="dict.type.assign_orders_status" @onSigin="onSigin" @success="onRefresh"
				@authEvent="handleAuth" />
		</scroll-view>
		<SignInTip ref="signInTip" @success="onRefresh" />
		<AuthModal ref="authModal" @success="onRefresh" />
		<TabBar current="order"></TabBar>
	</view>
</template>

<script>
	import ItemCard from "@/components/itemCard/index.vue";
	import SignInTip from "@/components/signInTip/index.vue";
	import TabBar from '@/components/tabBar/index.vue'
	import AuthModal from '@/components/authModal/index.vue'
	import {
		mapGetters
	} from 'vuex'

	export default {
		dicts: ["assign_orders_status"],
		components: {
			ItemCard,
			SignInTip,
			TabBar,
			AuthModal
		},
		computed: {
			...mapGetters(['theme'])
		},
		data() {
			return {
				list: [
					{
						name: "指派订单",
						value: 0
					},{
						name: "公海订单",
						value: 1
					},
				],
				subList: [{
						name: "全部",
						value: null
					},
					// {
					// 	name: "待作业",
					// 	value: 3
					// 	// badge: {
					// 	//   value: 5,
					// 	// },
					// },
					{
						name: "进行中",
						value: 4
					},
					{
						name: "待审核",
						value: 5
					},
					// {
					// 	name: "待支付",
					// 	value: 7
					// },
					{
						name: "已完成",
						value: '9'
					},
				],
				subCurrent: 0,
				currentType: 0,
				dataList: [],
				typeStatus: null,
				triggered: false,
				finish: false,
				loading: false,
				pageNum: 1,
				// 0指派 1公海 2全部
				orderType: 0
			};
		},
		onLoad() {
		},
		onShow(option) {
			if (this.theme == 'farmer') {
				uni.setNavigationBarColor({
					backgroundColor: '#0BCA89',
					frontColor: '#ffffff',
				});
				this.orderType = 1
				this.subList = [{
						name: "全部",
						value: null
					},
					{
						name: "待接单",
						value: 1
					},
					{
						name: "进行中",
						value: 4
					},
					{
						name: "待审核",
						value: 5
					},
					// {
					// 	name: "待评价",
					// 	value: 8
					// },
					{
						name: "已完成",
						value: '9'
					},
				]
			} else {
				uni.setNavigationBarColor({
					backgroundColor: '#006BFE',
					frontColor: '#ffffff',
				});
				this.subList = [{
						name: "全部",
						value: null
					},
					{
						name: "进行中",
						value: 4
					},
					{
						name: "待审核",
						value: 5
					},
					{
						name: "已完成",
						value: '9'
					},
				]
				this.currentType = 0
				this.orderType = 0
			}
			const orderTab = uni.getStorageSync('orderTab')
			if (orderTab) {
				if(this.theme == 'fs') {
					this.currentType = Number(orderTab)
					this.orderType = Number(orderTab)
					this.subCurrent = 3
					this.typeStatus = '9'
				} else {
					this.subCurrent = Number(orderTab)
					this.typeStatus = orderTab == 0 ? null : '9'
				}
				uni.removeStorageSync('orderTab')
			}
			this.onRefresh();
		},
		methods: {
			// 类型tab切换
			onChange(v) {
				this.currentType = v.index
				this.orderType = v.value
				this.resetAndLoad();
			},
			// 状态tab切换
			onSubChange(v) {
				// console.log('状态切换', v);
				this.subCurrent = v.index
				this.typeStatus = v.value;
				this.resetAndLoad();
			},
			// 重置分页、标记未结束，然后拉第一页
			resetAndLoad() {
				this.finish = false;
				this.pageNum = 1;
				this.loadList();
			},
			onPulling(e) {},
			onRefresh() {
				this.triggered = true;
				this.finish = false;
				this.pageNum = 1;
				this.loadList();
			},
			// onRestore() {
			// 	this.triggered = "restore"; // 需要重置
			// 	console.log("onRestore");
			// },
			onAbort() {
				console.log("onAbort");
				this.triggered = "restore"
			},
			onScrolltolower() {
				if (this.loading) return;
				if (this.finish) return;
				this.pageNum++;
				this.loadList();
			},
			loadList() {
				this.loading = true;
				this.$http
					.post(
						"/cadre/assignOrder/listByflyId", {}, {
							params: {
								// highSeas: '0',
								orderType: this.orderType,
								pageNum: this.pageNum,
								pageSize: 4,
								status: this.typeStatus,
								theme: this.theme == 'fs' ? 2 : 1,
								// 经度（浮点型）
								lng: null,
								// 纬度（浮点型）
								lat: null,
								...(this.$hd.position || {}),
							},
							custom: {
								toast: true
							}
						}
					)
					.then((res) => {
						this.loading = false;
						if (this.pageNum === 1) {
							this.dataList = res.data.rows;
						} else {
							this.dataList = [...this.dataList, ...res.data.rows];
						}

						if (this.dataList.length == res.data.total) {
							this.finish = true;
						}
						this.triggered = false;
					})
					.catch((err) => {
						this.triggered = false;
						// this.triggered = "restore"
						console.log("err", err);
					});
			},
			// 签到
			onSigin(data) {
				this.$refs.signInTip.show(data);
			},
			// 完工审核
			handleAuth(data) {
				this.$refs.authModal.show(data);
			}
		},
	};
</script>

<style scoped>
	.list-box {
		height: calc(100vh - 90px);
		padding: 20rpx;
	}
</style>